﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]   
    public class TaxItemManagerController : AdminBaseController
    {
        //
        // GET: /FinanceProp/
        /// <summary>
        /// 代码维护主页
        /// </summary>
        /// <returns>视图</returns>
        [AuthorityActionFilter("m_TaxItemManager", Order = 1)]
        [LogPageActionFilter("税目管理", Order = 2)]
        public ActionResult Index()
        {
            List<Model.TaxItem> codeNameList = new List<Model.TaxItem>();
            ViewData["blcode"] = true;
            BLL.TaxItem codeNameBll = new BLL.TaxItem();
            ViewData["TaxClassList"] = new BLL.TaxClass().GetModelList("");
            codeNameList = codeNameBll.GetModelList("");
            codeNameList = codeNameList.OrderBy(s => s.FTaxClassNo).ToList();
            return View(codeNameList);
        }

        /// <summary>
        /// 编码树相关调用方法 主要用于刷新
        /// </summary>
        /// <returns>视图</returns>
        public ActionResult TaxItemListPartial()
        {
            BLL.TaxItem codeNameBll = new BLL.TaxItem();
            List<Model.TaxItem> codeNameList = new List<Model.TaxItem>();
            string taxClass = Request.Params["taxClass"];
            string FTaxItemName = Request.Params["FTaxItemName"];
            string FTaxItemNo = Request.Params["FTaxItemNo"];
            string strwhere = "1=1 ";
            if (taxClass != "")
            {
                strwhere += " and a.FTaxClassNo='" + taxClass + "'";
            }
            if (FTaxItemName != "")
            {
                strwhere += " and FTaxItemName like '%" + FTaxItemName + "%'";
            }
            if (FTaxItemNo != "")
            {
                strwhere += " and FTaxItemNo like '%" + FTaxItemNo + "%'";
            }
            codeNameList = codeNameBll.GetModelList(strwhere);
            ViewData["blcode"] = true;
            codeNameList = codeNameList.OrderBy(s => s.FTaxClassNo).ToList();
            return PartialView("TaxItemListPartial", codeNameList);
        }

        /// <summary>
        /// 编码增加修改
        /// </summary>
        /// <param name="codeClsNo">编码类别编码 必须有具体值</param>
        /// <param name="codeNo">编码</param>
        /// <returns>视图</returns>
        public ActionResult TaxItemEdit(string taxClassNo,string taxItemNo)
        {
            Model.TaxItem codeNameObj = new Model.TaxItem();
            BLL.TaxClass taxClassBLL = new BLL.TaxClass();
            ViewData["blcodeNo"] = true;
            ViewData["TaxClassList"] = taxClassBLL.GetModelList("");
            if (null != taxClassNo)
            {
                taxClassNo = Fetch.estimateString(taxClassNo, CheckGetEnum.Safety);
                BLL.TaxItem codeNameBll = new BLL.TaxItem();
                codeNameObj = codeNameBll.GetModel(taxItemNo,taxClassNo);
                if (null == codeNameObj)
                {
                    codeNameObj = new Model.TaxItem();
                }
                else
                {
                    ViewData["blcodeNo"] = false;
                }
            }

            return View(codeNameObj);
        }


        /// <summary>
        /// 编码保存
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeName">编码名字</param>
        /// <param name="codeClsNo">编码类别编码</param>
        /// <param name="isAdd">是否添加true添加，false修改</param>
        /// <returns>是否添加成功</returns>
        public string TaxItemSave(string taxItemNo, string taxItemName, string taxClassNo, string nationalTaxItemName, string isUse, bool isAdd)
        {
            string str = string.Empty;

            taxItemNo = Fetch.estimateString(taxItemNo, CheckGetEnum.Safety);
            taxItemName = Fetch.estimateString(taxItemName, CheckGetEnum.Safety);
            nationalTaxItemName = Fetch.estimateString(nationalTaxItemName, CheckGetEnum.Safety);

            BLL.TaxItem codeNameBll = new BLL.TaxItem();
            if (isAdd)//增加时判断编码是否已存在
            {
                if (codeNameBll.Exists(taxItemNo, taxClassNo))
                {
                    return "exists";
                }
            }

            Model.TaxItem codeNameObj = new Model.TaxItem();
            codeNameObj.FTaxItemNo = taxItemNo;
            codeNameObj.FTaxItemName = taxItemName;
            codeNameObj.FTaxClassNo = taxClassNo;
            codeNameObj.FNationalTaxItemName = nationalTaxItemName;
            codeNameObj.FIsUse = isUse;

            try
            {
                if (isAdd)
                {
                    codeNameBll.Add(codeNameObj);
                }
                else
                {
                    codeNameBll.Update(codeNameObj);
                }
                str = "ok";

            }
            catch (Exception)
            {

                str = "error";
            }

            return str;
        }


        /// <summary>
        /// 删除编码数据
        /// </summary>
        /// <param name="codeNo">编码编号</param>
        /// <param name="codeClsNO">编码类别编码</param>
        /// <returns>是否删除成功</returns>
        public string DelTaxItem(string taxItemNo,string taxClassNo)
        {
            string str = string.Empty;
            if (null != taxItemNo)
            {
                taxItemNo = Fetch.estimateString(taxItemNo, CheckGetEnum.Safety);
                BLL.TaxItem codeNameBll = new BLL.TaxItem();
                try
                {
                    codeNameBll.Delete(taxItemNo, taxClassNo);
                    str = "ok";
                }
                catch (Exception)
                {
                    str = "error";
                }
            }
            return str;
        }

    }
}
